<script setup>
import PageContainer from "@/components/PageContainer.vue";
import { ref } from "vue";
import { useUserStore } from '@/stores/index'
import { userUpdateInfoService } from "@/api/user";
import { ElMessage } from "element-plus";

const userStore = useUserStore()
// const title=ref("基本资料")
const form = ref({
  username: null,
  nickname: null,
  password: null
})
const rules = {
  nickname: [
    { pattern: /^\S{2,10}$/, message: '昵称长度在2-10个字符', trigger: 'blur' },
    { required: true, message: '请输入用户昵称', trigger: 'blur' }
  ]
}
form.value.nickname = userStore.user.nickname
form.value.password = userStore.user.password
form.value.username = userStore.user.username

const onSubmit = async () => {
  await formRef.value.validate()
  // 提交修改
  await userUpdateInfoService(form.value)
  // 更新用户数据
  await userStore.getUser()
  ElMessage.success('修改成功')
}

const formRef = ref()
</script>

<template>
  <PageContainer title="基本资料">
    <el-form :model="form" :rules="rules" ref="formRef">
      <el-form-item label="登录名称" class="form-input-user">
        <el-input v-model="form.username" disabled></el-input>
      </el-form-item>
      <el-form-item label="用户名称" prop="nickname">
        <el-input v-model="form.nickname"></el-input>
      </el-form-item>
      <el-form-item label="用户密码" class="form-input-user">
        <el-input v-model="form.password" disabled></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="submit" @click="onSubmit">提交修改</el-button>
      </el-form-item>
    </el-form>
  </PageContainer>
</template>

<style scoped>
.submit {
  margin-left: 70px;
}

.form-input-user {
  margin-left: 9px;
}
</style>
